const rob = nums => {
  let n = nums.length
  if (n === 0) return 0
  if (n === 1) return nums[0]
  if (n === 2) return Math.max(nums[0], nums[1])
  if (n === 3) return Math.max(nums[0] + nums[2], nums[1])
  let p = nums[0],
    q = nums[1]
  for (let i = 2; i < n; i++) {
    let next = p
    if (q > p) {
      p = q
    }
    q = next + nums[i]
  }
  return p > q ? p : q
}

console.log(rob([2, 1, 1, 2]));